<template>
  <div>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="类型">
                <a-select
                  v-model="queryParam.type"
                  placeholder="请选择"
                >
                  <a-select-option v-for="(type, id) in revenuesType" :value="id" :key="id">{{ type.text }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="代理手机号">
                <a-input v-model="queryParam.phone" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="时间">
                <a-range-picker :allowClear="true" ref="dateRangePicker" @change="dateRangePickerChange"/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <span class="table-page-search-submitButtons">
                <a-button
                  type="primary"
                  @click="refreshList"
                >查询</a-button>
                <a-button style="margin-left: 8px" @click="handleReset">重置</a-button>
                <a-button style="margin-left: 8px" @click="handleExport">导出</a-button>
                <a-button style="margin-left: 8px" @click="handleExport(true)">大数据导出</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <s-table
        ref="pendingTable"
        size="default"
        rowKey="id"
        :columns="columns"
        :data="loadData"
        showPagination="auto"
      />
    </a-card>
  </div>
</template>

<script>
import { STable } from '@/components'
import PartnersForm from '@/views/proxy/partner/Form'
import { getFinanceList } from '@/api/finance'
import { mapGetters } from 'vuex'
import { formatAmount } from '@/utils/util'

export default {
    name: 'List',
    components: {
      PartnersForm,
      STable
    },
    computed: {
      ...mapGetters(['revenuesType'])
    },
    data () {
      return {
        // 查询参数
        queryParam: {},
        columns: [
          {
            title: '序号',
            dataIndex: 'id'
          },
          {
            title: '类型',
            customRender: item => this.revenuesType[item.type].text || ''
          },
          {
            title: '金额',
            customRender: item => formatAmount(((item.revenues_type === 2) ? -1 : 1) * item.amount)
          },
          {
            title: '代理手机号',
            dataIndex: 'agent.phone'
          },
          {
            title: '创建时间',
            dataIndex: 'created_at'
          },
          {
            title: '操作后余额',
            customRender: item => formatAmount(item.balance + item.unextractable_balance)
          },
          {
            title: '关联订单号',
            dataIndex: 'source.numbering'
          },
          {
            title: '代付订单号',
            dataIndex: ''
          }
        ],
        // 加载数据方法 必须为 Promise 对象
        loadData: parameter => {
          return getFinanceList(Object.assign(parameter, this.queryParam))
        }
      }
    },
    methods: {
      handleReset () {
        this.queryParam = {}
        this.$refs.dateRangePicker.$refs.picker.clearSelection(new MouseEvent('click'))
      },
      dateRangePickerChange (moment, date) {
        this.queryParam.created_at_begin = date[0]
        this.queryParam.created_at_end = date[1]
      },
      refreshList () {
        this.pendingSelectedRowKeys = []
        this.$refs.pendingTable.refresh(true)
      },
      handleExport (isAsync = false) {
        const exportParam = { export: true }
        ;(isAsync === true) && Object.assign(exportParam, { async: true })

        getFinanceList(Object.assign(exportParam, this.queryParam)).then(_ => {
          (isAsync === true) && this.$notification.success({ message: '添加任务成功,请关注[系统-异步导出任务]' })
        })
      }
    }
  }
</script>
